#if OpenMedia
using System;
using UnityEngine;
//using Voodoo.Sauce.Internal.IAP;

public class OpenMeidaController :MonoBehaviour, AdController
{
    public enum AdType
    {
        Test,
        Android,
        IOS,
    }

    AdType curAdType
    {
        get
        {
            if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                return AdType.IOS;
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                return AdType.Android;
            }
            else
            {
                GameDebug.LogError($"ad current unsupport platform:{Application.platform}");
                return AdType.Test;
            }
        }
    }
    string splashAdUnitId
    {
        get
        {
            if (curAdType == AdType.Android)
                return "12490";
            else if (curAdType == AdType.IOS)
                return "12494";
            else
                return "";
        }
    }
    string bannerAdUnitId
    {
        get
        {
            if (curAdType == AdType.Android)
                return "13070";
            else if (curAdType == AdType.IOS)
                return "12493";
            else
                return "";
        }
    }
    
    string interstitialAdUnitId
    {
        get
        {
            if (curAdType == AdType.Android)
                return "13069";
            else if (curAdType == AdType.IOS)
                return "12492";
            else
                return "";
        }
    }
    
    string rewardAdUnitId
    {
        get
        {
            if (curAdType == AdType.Android)
                return "13068";
            else if (curAdType == AdType.IOS)
                return "12491";
            else
                return "";
        }
    }
    string appKey
    {
        get
        {
            if (curAdType == AdType.Android)
                return "o1b4XeFAozYcMMLOhyEXdq5X8TO47Lw3";
            else if (curAdType == AdType.IOS)
                return "m5N9Kg352tFJDdNopOXpOuGDqVffYEef";
            else
                return "";
        }
    }
    bool m_InterstitialLoaded;
    bool m_InterstitialClosed;


    bool m_VideoLoaded;
    bool m_VideoClosed;
    bool m_VideoSuc;

    private bool m_DisableInterAndBanner;

    private void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }

    public void Init()
    {
        Debug.Log($"OpenMedia Init splashId:{splashAdUnitId} bannerAdUnitId :{this.bannerAdUnitId}  interstitialAdUnitId :{this.interstitialAdUnitId}  rewardAdUnitId :{this.rewardAdUnitId}");
        
       // m_DisableInterAndBanner = VoodooPremium.IsPremium();
        
        OmEvents.onSdkInitSuccessEvent += SdkInitSuccessEvent;
        OmEvents.onSdkInitFailedEvent += SdkInitFailedEvent;

        Om.Agent.init(appKey); 
        
        Om.Agent.setGDPRConsent(true);

        OmEvents.onRewardedVideoAvailabilityChangedEvent += RewardedVideoAvailabilityChangedEvent;
        OmEvents.onRewardedVideoShowedEvent += RewardedVideoAdShowedEvent;
        OmEvents.onRewardedVideoShowFailedEvent += RewardedVideoAdShowFailedEvent;
        OmEvents.onRewardedVideoStartedEvent += RewardedVideoAdStartedEvent;
        OmEvents.onRewardedVideoEndedEvent += RewardedVideoAdEndedEvent;
        OmEvents.onRewardedVideoRewardedEvent += RewardedVideoAdRewardedEvent;
        OmEvents.onRewardedVideoClickedEvent += RewardedVideoAdClickedEvent;
        OmEvents.onRewardedVideoClosedEvent += RewardedVideoAdClosedEvent;

        OmEvents.onInterstitialAvailabilityChangedEvent += InterstitialAdAvailabilityChangedEvent;
        OmEvents.onInterstitialShowedEvent += InterstitialAdShowedEvent;
        OmEvents.onInterstitialShowFailedEvent += InterstitialAdShowFailedEvent;
        OmEvents.onInterstitialClickedEvent += InterstitialAdClickedEvent;
        OmEvents.onInterstitialClosedEvent += InterstitialAdClosedEvent;

        OmEvents.onBannerLoadSuccessEvent += BannerLoadSuccessEvent;
        OmEvents.onBannerLoadFailedEvent += BannerLoadFailedEvent;
        OmEvents.onBannerClickedEvent += BannerClickedEvent;

        OmEvents.onSplashAdLoadSuccessEvent += SplashAdLoadSuccessEvent;
        OmEvents.onSplashAdLoadFailedEvent += SplashAdLoadFailedEvent;
        OmEvents.onSplashAdShowedEvent += SplashAdShowedEvent;
        OmEvents.onSplashAdClickEvent += SplashAdClickEvent;
        OmEvents.onSplashAdClosedEvent += SplashAdClosedEvent;
        OmEvents.onSplashAdShowFailedEvent += SplashAdShowFailedEvent;

#if DEBUG
        Om.Agent.debug(true);
#else
        Om.Agent.debug(false);
#endif
    }

    private void SdkInitSuccessEvent()
    {
        GameDebug.LogError($"open media inti suc");

        //LoadSplash();
        //OnSlashEnd();
        
        LoadBanner();

        DoActionDelay.DelayAction(1, () =>
        {
            LoadVideoAd();

            DoActionDelay.DelayAction(1,LoadInterstitialAd);
        });
    }


    private void SdkInitFailedEvent(string obj)
    {
        GameDebug.LogError($"open media inti fail:{obj}");
        //MGame.instance.BeginGameContent();
    }

#region splash

    public void LoadSplash()
    {
        Debug.Log("open media LoadSplash");

        Om.Agent.loadSplashAd(splashAdUnitId);
    }


    public void ShowSplash()
    {
        if (Om.Agent.isPromotionAdReady())
        {
            Om.Agent.showPromotionAd(132, 0, 0, 0, 0);
        }
    }

    void SplashAdLoadSuccessEvent(string placementId)
    {
        Debug.Log("unity-script: I got SplashAdLoadSuccessEvent: " + placementId);

        ShowSplash();
    }

    void SplashAdLoadFailedEvent(string placementId)
    {
        Debug.Log("unity-script: I got SplashAdLoadFailedEvent: " + placementId);

        OnSlashEnd();
    }

    void SplashAdShowedEvent(string placementId)
    {
        Debug.Log("unity-script: I got SplashAdShowedEvent: " + placementId);
        OnSlashEnd();
    }

    void SplashAdShowFailedEvent(string placementId)
    {
        Debug.Log("unity-script: I got SplashAdShowFailedEvent: " + placementId);
        OnSlashEnd();
    }

    void SplashAdClickEvent(string placementId)
    {
        Debug.Log("unity-script: I got SplashAdClickEvent: " + placementId);
    }

    void SplashAdClosedEvent(string placementId)
    {
        Debug.Log("unity-script: I got SplashAdClosedEvent: " + placementId);
        OnSlashEnd();
    }

    void OnSlashEnd()
    {
        /*MGame.instance.BeginGameContent();
        LoadBanner();*/
    }
#endregion

#region RewardAd

    public void LoadVideoAd()
    {
        Debug.Log("OpenMedia -> LoadVideoAd");
    }

    public void PlayVideoAd()
    {
        Debug.Log("OpenMedia -> PlayerVideo");

        m_VideoSuc = false;
        
        if (Om.Agent.isRewardedVideoReady())
        {
            Om.Agent.showRewardedVideo("Battle");

            m_VideoLoaded = false;
        }
        else
        {
            GameDebug.LogError("Video ad has not ready");
            this.onVideoAdFailedToShow?.Invoke(Localization.instance.GetValue("ad_not_ready"));

        }
    }


    private void RewardedVideoAvailabilityChangedEvent(bool obj)
    {
        Debug.Log($"RewardedVideoAvailabilityChangedEvent:{obj}");
        if (obj)
        {
            m_VideoLoaded = true;
            m_VideoClosed = false;

            this.onVideoAdLoaded?.Invoke("");
        }
        else
        {
            //this.onVideoAdFailedToShow?.Invoke("");

            //LoadVideoAd();
        }
    }

    void RewardedVideoAdShowedEvent(string scene)
    {
        Debug.Log("unity-script: I got RewardedVideoAdOpenedEvent: " + scene);
    }
    void RewardedVideoAdRewardedEvent(string scene)
    {
        Debug.Log("unity-script: I got RewardedVideoAdRewardedEvent: " + scene);

        if (m_VideoClosed)
        {
            this.onVideoPlaySuc?.Invoke($"");

            return;
        }
        m_VideoSuc = true;

    }

    void RewardedVideoAdClosedEvent(string scene)
    {
        Debug.Log($"Rewarded video ad did close  m_VideoSuc:{m_VideoSuc}");

        if (m_VideoSuc)
        {
            this.onVideoPlaySuc?.Invoke($"");
            return;
        }

        m_VideoClosed = true;

        this.onVideoAdClosed?.Invoke();

       // LoadVideoAd();
    }

    void RewardedVideoAdStartedEvent(string scene)
    {
        Debug.Log("unity-script: I got RewardedVideoAdStartedEvent: " + scene);
    }
    void RewardedVideoAdEndedEvent(string scene)
    {
        Debug.Log("unity-script: I got RewardedVideoAdEndedEvent: " + scene);
    }
    void RewardedVideoAdShowFailedEvent(string error)
    {
        Debug.Log("unity-script: I got RewardedVideoAdShowFailedEvent, error: " + error);

        this.onVideoAdFailedToLoad?.Invoke(error);
    }
    void RewardedVideoAdClickedEvent(string scene)
    {
        Debug.Log("unity-script: I got RewardedVideoAdClickedEvent, name = " + scene);

        this.onVideoAdOpening?.Invoke();
    }



#endregion

#region Interstitial

    public void LoadInterstitialAd()
    {
        if (m_DisableInterAndBanner)
            return;
        
        Debug.Log("OpenMedia -> LoadInterstitialAd");
    }
    
    public void ShowInterstitialAd()
    {
        if (m_DisableInterAndBanner)
            return;
        
        Debug.Log("OpenMedia -> ShowInterstitialAd");

        if (Om.Agent.isInterstitialReady())
        {
            Om.Agent.showInterstitial("Result");
        }
        else
        {
            GameDebug.LogError("Interstitial has not ready");

            this.onInterstitialAdFail?.Invoke("");
        }
    }

    void InterstitialAdAvailabilityChangedEvent(bool available)
    {
        Debug.Log("unity-script: I got InterstitialAdAvailabilityChangedEvent: " + available);
        if (available)
        {
            Debug.Log("Interstitial ad loaded.");

            m_InterstitialLoaded = true;
            m_InterstitialClosed = false;

            this.onInterstitialAdLoaded?.Invoke("");
        }
        else
        {
            Debug.Log("Interstitial ad failed to load with error: unavailable");

            this.onInterstitialAdFailedToLoad?.Invoke("unavailable");
        }
    }
    void InterstitialAdShowFailedEvent(string error)
    {
        Debug.Log("unity-script: I got InterstitialAdShowFailedEvent, code: " + error);

        Debug.Log("Interstitial ad failed to load with error: ");

        this.onInterstitialAdFail?.Invoke("");
    }

    void InterstitialAdClickedEvent(string scene)
    {
        Debug.Log("Interstitial ad clicked.");

        this.onInterstitialAdOpening?.Invoke("");
    }

    void InterstitialAdShowedEvent(string scene)
    {
        Debug.Log("unity-script: I got InterstitialAdShowedEvent: " + scene);
    }

    void InterstitialAdClosedEvent(string scene)
    {
        Debug.Log("Interstitial ad did close.");
        m_InterstitialClosed = true;

        if (this.onInterstitialAdClosed != null)
            this.onInterstitialAdClosed.Invoke("");
        else
            GameDebug.LogError("on interstitial close event null");
    }


    //public void HandleOnInterstitialAdLeavingApplication(object sender, EventArgs args)
    //{
    //    MonoBehaviour.print("OpenMedia HandleAdLeavingApplication event received");

    //    this.onInterstitialAdLeavingApplication?.Invoke("");
    //}
#endregion

#region Banner

    bool m_ShowedBanner;
    public bool showedBanner => m_ShowedBanner;

    public void LoadBanner()
    {
        #if NoBanner
        return;
        #endif
        
        if (m_DisableInterAndBanner)
            return;

        if (!m_ShowedBanner)
            Om.Agent.loadBanner(bannerAdUnitId, AdSize.BANNER, BannerPostion.BOTTOM);
    }

    public void DisableBannerAndInterstitial()
    {
        m_DisableInterAndBanner = true;
        
        this.HideBanner();
    }

    public void EnableBannerAndInterstitial()
    {
        m_DisableInterAndBanner = false;
        
        LoadBanner();
        LoadInterstitialAd();
    }

    public void ShowBanner()
    {
#if NoBanner
        return;
#endif
        
        if (m_DisableInterAndBanner)
            return;
        
        Debug.Log("OpenMedia -> ShowBanner");
        if (!m_ShowedBanner)
        {
            Om.Agent.loadBanner(bannerAdUnitId, AdSize.BANNER, BannerPostion.BOTTOM);
        }
    }

    public void HideBanner()
    {
        Debug.Log("OpenMedia -> HideBanner");
        
        Om.Agent.hideBanner(bannerAdUnitId);
    }

    void BannerLoadSuccessEvent(string placementId)
    {
        Debug.Log("Banner loaded");
        m_ShowedBanner = true;

        this.onBannerAdLoaded?.Invoke("");


        /*
        if (VoodooPremium.IsPremium())
        {
            Debug.Log("IsPremium, no Banner");
        }
        else*/
        {
            Om.Agent.displayBanner(bannerAdUnitId);
        }
    }

    void BannerLoadFailedEvent(string placementId, string error)
    {
        Debug.Log("Banner failed to load with error: " + error);

        this.onBannerAdFailedToLoad?.Invoke(error);
    }
    void BannerClickedEvent(string placementId)
    {
        Debug.Log("Banner clicked.");

        this.onBannerAdOpening?.Invoke("");
    }


    //public void HandleOnBannerAdClosed(object sender, EventArgs args)
    //{
    //    MonoBehaviour.print("OpenMedia HandleAdClosed event received");

    //    this.onBannerAdClosed?.Invoke("");
    //}

    //public void HandleOnBannerAdLeavingApplication(object sender, EventArgs args)
    //{
    //    MonoBehaviour.print("OpenMedia HandleAdLeavingApplication event received");

    //    this.onBannerAdLeavingApplication?.Invoke("");
    //}

#endregion


    public void Release()
    {

        HideBanner();
    }

    public void ReleaseBannerEvent()
    {
        onBannerAdLoaded = null;
        onBannerAdFailedToLoad  = null;
        onBannerAdOpening = null;
        onBannerAdClosed  = null;
        onBannerAdLeavingApplication  = null;
    }

    public void ReleaseInterstitialEvent()
    {
        GameDebug.LogError($"ReleaseInterstitialEvent");

        onInterstitialAdLoaded = null;
        onInterstitialAdFailedToLoad = null;
        onInterstitialAdOpening = null;
        onInterstitialAdClosed = null;
        onInterstitialAdLeavingApplication = null;
    }

    public void ReleaseVideoEvent()
    {
        onVideoAdLoaded  = null;
        onVideoAdFailedToLoad  = null;
        onVideoAdOpening   = null;
        onVideoAdFailedToShow  = null;
        onVideoPlaySuc  = null;
        onVideoAdClosed  = null;
    }



    private Action<string> m_OnBannerAdLoaded;
    public Action<string>onBannerAdLoaded
    {
        get { return m_OnBannerAdLoaded;}
        set { m_OnBannerAdLoaded = value; }
    }

    // Called when an Banner ad request failed to load.
    public Action<string> m_OnBannerAdFailedToLoad; 
    public Action<string>onBannerAdFailedToLoad
    {
        get { return m_OnBannerAdFailedToLoad;}
        set { m_OnBannerAdFailedToLoad = value; }
    }

    public Action<string> onBannerShowSuc { get; set; }
    public Action<string> onBannerShowFailed { get; set; }

    // Called when an Banner ad is clicked.
    public Action<string> m_OnBannerAdOpening;
    public Action<string>onBannerAdOpening
    {
        get { return m_OnBannerAdOpening;}
        set { m_OnBannerAdOpening = value; }
    }
    // Called when the user returned from the app after an Banner ad click.
    public Action<string> m_OnBannerAdClosed;
    public Action<string>onBannerAdClosed
    {
        get { return m_OnBannerAdClosed;}
        set { m_OnBannerAdClosed = value; }
    }
    // Called when the Banner ad click caused the user to leave the application.
    public Action<string> m_OnBannerAdLeavingApplication;
    public Action<string>onBannerAdLeavingApplication
    {
        get { return m_OnBannerAdLeavingApplication;}
        set { m_OnBannerAdLeavingApplication = value; }
    }
	 
    // Called when an Interstitial ad request has successfully loaded.
    public Action<string> m_OnInterstitialAdLoaded;
    public Action<string>onInterstitialAdLoaded
    {
        get { return m_OnInterstitialAdLoaded;}
        set { m_OnInterstitialAdLoaded = value; }
    }
    // Called when an Interstitial ad request failed to load.
    public Action<string> m_OnInterstitialAdFailedToLoad;
    public Action<string>onInterstitialAdFailedToLoad
    {
        get { return m_OnInterstitialAdFailedToLoad;}
        set { m_OnInterstitialAdFailedToLoad = value; }
    }

    public Action<string> onInterstitialAdSuc { get; set; }
    public Action<string> onInterstitialAdFail { get; set; }

    // Called when an Interstitial ad is shown.
    public Action<string> m_OnInterstitialAdOpening;
    public Action<string>onInterstitialAdOpening
    {
        get { return m_OnInterstitialAdOpening;}
        set { m_OnInterstitialAdOpening = value; }
    }
    // Called when the Interstitial ad is closed.
    public Action<string> m_OnInterstitialAdClosed;
    public Action<string>onInterstitialAdClosed
    {
        get { return m_OnInterstitialAdClosed;}
        set { m_OnInterstitialAdClosed = value; }
    }
    // Called when the Interstitial ad click caused the user to leave the application.
    public Action<string> m_OnInterstitialAdLeavingApplication;
    public Action<string>onInterstitialAdLeavingApplication
    {
        get { return m_OnInterstitialAdLeavingApplication;}
        set { m_OnInterstitialAdLeavingApplication = value; }
    }

    public Action<string> m_OnVideoAdLoaded;
    public Action<string>onVideoAdLoaded
    {
        get { return m_OnVideoAdLoaded;}
        set { m_OnVideoAdLoaded = value; }
    }
    // Called when an ad request failed to load.
    public Action<string> m_OnVideoAdFailedToLoad;
    public Action<string>onVideoAdFailedToLoad
    {
        get { return m_OnVideoAdFailedToLoad;}
        set { m_OnVideoAdFailedToLoad = value; }
    }
    // Called when an ad is shown.
    public Action m_OnVideoAdOpening;
    public Action onVideoAdOpening
    {
        get { return m_OnVideoAdOpening;}
        set { m_OnVideoAdOpening = value; }
    }
    // Called when an ad request failed to show.
    public Action<string> m_OnVideoAdFailedToShow;
    public Action<string>onVideoAdFailedToShow
    {
        get { return m_OnVideoAdFailedToShow;}
        set { m_OnVideoAdFailedToShow = value; }
    }
    // Called when the user should be rewarded for interacting with the ad.
    public Action<string> m_OnVideoUserEarnedReward;
    public Action<string>onVideoPlaySuc
    {
        get { return m_OnVideoUserEarnedReward;}
        set { m_OnVideoUserEarnedReward = value; }
    }
    // Called when the ad is closed.
    public Action  m_OnVideoAdClosed;
    public Action onVideoAdClosed
    {
        get { return m_OnVideoAdClosed;}
        set { m_OnVideoAdClosed = value; }
    }

    public bool IsVideoReady => Om.Agent.isRewardedVideoReady(); 
    public bool IsInterstitialReady => Om.Agent.isInterstitialReady();

    public void ShowTestSuite()
    {
        Debug.Log($"ShowTestSuite!");

        //OMTestSuite.Show(appKey);
    }


    void Update()
    {

    }
}

#endif